﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_Material : BaseXtraForm
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_Material()
        {
            InitializeComponent();

            this.BindCheckEdit(MaterialView, false, true);
            this.BindRowIndicator(MaterialView);
            this.SetTreeListStyle(treeList1);
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Material_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            treeList1.DataSource = rservice.GetAllMaterialCategory();
            repositoryItemSearchLookUpEdit1.DataSource = rservice.GetAllUnit();
            repositoryItemSearchLookUpEdit2.DataSource = rservice.GetAllSuppliers();
        }

        /// <summary>
        /// 右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            }
        }

        /// <summary>
        /// 焦点行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            RepastService rservice = new RepastService();
            var focusCategory = treeList1.FocusedNode.GetValue("ID");
            MaterialGrid.DataSource = rservice.GetMaterialByCatetoryID((Guid)focusCategory);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var focesCategoryId = treeList1.FocusedNode.GetValue("ID");

            if (focesCategoryId == null)
            {
                ShowInfo("请先添加分类!");
                return;
            }

            Frm_AddMaterial frm = new Frm_AddMaterial(status:EStatus.Add,id: (Guid)focesCategoryId);
            frm.SaveEvent += Material_AddSaveEvent;
            frm.ShowDialog();
        }
        private void Material_AddSaveEvent(Object sender,EventArgs e)
        {
            RepastService rservice = new RepastService();
            var focesCategoryId = treeList1.FocusedNode.GetValue("ID");
            MaterialGrid.DataSource = rservice.GetMaterialByCatetoryID((Guid)focesCategoryId);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var focusMaterial = MaterialView.GetFocusedRow() as Material;
            if (focusMaterial == null)
            {
                ShowInfo("请选择需要编辑的物料!");
                return;
            }

            Frm_AddMaterial frm = new Frm_AddMaterial(status:EStatus.Modify,id:focusMaterial.ID);
            frm.SaveEvent += Material_UpdateSaveEvent;
            frm.ShowDialog();
        }
        private void Material_UpdateSaveEvent(Object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            Material material = sender as Material;
            if (material != null)
            {
                MaterialView.SetFocusedRowCellValue("Code", material.Code);
                MaterialView.SetFocusedRowCellValue("Name", material.Name);
                MaterialView.SetFocusedRowCellValue("UnitID", material.UnitID);
                MaterialView.SetFocusedRowCellValue("SupplierID",material.SupplierID);
                MaterialView.SetFocusedRowCellValue("Place", material.Place);
            }
            else
            {
                var focusCategory = treeList1.FocusedNode.GetValue("ID");
                MaterialGrid.DataSource = rservice.GetMaterialByCatetoryID((Guid)focusCategory);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RepastService rservice = new RepastService();
            var focesCategoryId = treeList1.FocusedNode.GetValue("ID");
            var material = this.GetChecked<Material>(MaterialView);
            if (material.Count == 0)
            {
                ShowInfo("请选择需要删除的物料!");
                return;
            }

            var materialId = material.Select(p => p.ID).ToArray();
            if (ShowQuestion($"是否删除{material.Count}条数据"))
            {
                int count = rservice.DelMaterial(materialId);
                MaterialGrid.DataSource = rservice.GetMaterialByCatetoryID((Guid)focesCategoryId);
                ShowInfo("删除成功!");
            }
        }
       
    }
}
